package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.CompanyForm;
import com.vcaresolution.logistic.pojo.Branch;
import com.vcaresolution.logistic.pojo.City;
import com.vcaresolution.logistic.pojo.Company;
import com.vcaresolution.logistic.to.BranchTO;
import com.vcaresolution.logistic.to.CompanyTO;
import com.vcaresolution.logistic.to.DataHolderTO;
import com.vcaresolution.logistic.util.ApplicationConstants;
import com.vcaresolution.util.Utils;

@Service
public class CompanyServiceImpl extends BaseService implements CompanyService{

	private Company populateCompanyPojo(CompanyForm companyForm){
		Company company = new Company();
		BeanUtils.copyProperties(companyForm, company);
		
		return company;
	}
	
	@Override
	@Transactional
	public void add(CompanyForm companyForm) {
		Company company = populateCompanyPojo(companyForm);
		
		company.setCity(getDaoFactory().getUtilityDAO().findById(City.class, companyForm.getCityId()));
		company.setCreationdate(Utils.getCurrentDate());
		company.setAuditdate(Utils.getCurrentDate());
		
		//Setting Default Status as Active 
		company.setIsactive(ApplicationConstants.ACTIVE_STATUS);
		
		getDaoFactory().getUtilityDAO().add(company);
	}

	@Override
	@Transactional
	public void update(CompanyForm companyForm) {
		Company company = populateCompanyPojo(companyForm);
		
		company.setCity(getDaoFactory().getUtilityDAO().findById(City.class, companyForm.getCityId()));
		company.setCreationdate(Utils.getCurrentDate());
		
		getDaoFactory().getUtilityDAO().update(company);
	}

	@Override
	@Transactional
	public void delete(CompanyForm companyForm) {
		getDaoFactory().getUtilityDAO().delete(populateCompanyPojo(companyForm));
	}

	@Override
	@Transactional
	public CompanyTO findById(Serializable companyID) {
		Company company = (Company) getDaoFactory().getUtilityDAO().findById(Company.class,companyID);
		
		CompanyTO companyTO = new CompanyTO();
		BeanUtils.copyProperties(company, companyTO);
		
		companyTO.setCityId(company.getCity().getCityid());
		
		return companyTO; 
	}

	@Override
	@Transactional
	public List<CompanyTO> findByAll() {
		List<Company> companyList = getDaoFactory().getUtilityDAO().findByAll(Company.class);
		
		List<CompanyTO> companyFormList = new ArrayList<CompanyTO>();
		
		if( companyList != null && !companyList.isEmpty() ){
			CompanyTO companyForm = null;
			for(Company company: companyList){
				companyForm = new CompanyTO();
				
				BeanUtils.copyProperties(company, companyForm);
				companyFormList.add(companyForm);
			}
		}	
		return companyFormList;
	}

	/*@Override
	public List<BranchTO> getAssociatedBranchList(Serializable companyId) {
		
		List<BranchTO> branchTOList = new ArrayList<BranchTO>();
		
		List<Branch> associatedBranchList = getDaoFactory().getCompanyDAO().getAssociatedBranchList(companyId);
		if( associatedBranchList != null && !associatedBranchList.isEmpty() ){
			BranchTO branchTO = null;
			for(Branch branch : associatedBranchList){
				branchTO = new BranchTO();
				
				BeanUtils.copyProperties(branch, branchTO);
				branchTOList.add(branchTO);
			}
		}
		
		return branchTOList;
	}*/
	
	/**#editedByVishal*/
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getAssociatedBranchList(Serializable companyId,Class<T> returnType) 
	{
		List<T> branchTOList = new ArrayList<T>();
		try
		{
			List<Branch> associatedBranchList = getDaoFactory().getCompanyDAO().getAssociatedBranchList(companyId);
			if( associatedBranchList != null && !associatedBranchList.isEmpty())
			{
				T instance = null;
				DataHolderTO dataHolder = null;
				for(Branch branch : associatedBranchList)
				{
					instance = returnType.newInstance();
					if(returnType == DataHolderTO.class)
					{
						dataHolder = (DataHolderTO)instance;
						dataHolder.setFieldId(branch.getBranchid());
						dataHolder.setFieldname(branch.getBranchname());
						branchTOList.add((T)dataHolder);
					}
					else if(returnType == BranchTO.class)
					{
						BeanUtils.copyProperties(branch, instance);
						branchTOList.add(instance);
					}
					else 
					{
						System.out.println(returnType.getName() + "is not valid class for return type # CompanyServiceImpl.getAssociatedBranchList");
					}
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return branchTOList;
	}
}